﻿using System;
using PT.NoteDetection.Util;

namespace PT.NoteDetection.Envelope {
    public class MovingAverage {

        readonly WindowFunction<float> w;
        readonly Func<float, float> op;
        float sum;

        public MovingAverage(WindowFunction<float> window, Func<float, float> sampleOperation) {
            w = window;
            op = sampleOperation;
        }

        public void Advance(float newData) {
            if (w.Count > 0) {
                sum -= op(w[0]);
            }

            w.Advance(newData);

            sum += op(newData);
        }

        public float Average() {
            return sum / w.Count;
        }

        public static float Power(float sample) {
            return sample * sample;
        }
    }
}
